// APP初始化
import {
    mapState
} from 'vuex';
import Vue from 'vue';
import manifest from '@/manifest.json';
module.exports = {
    data() {
        return {
            scroll: 0, // 滚动高度
            scrollInfo: {},
            manifest, // 应用配置信息
            isRipple: true, // 是否开启按钮水波纹效果
            mapKey: '', // 地图密钥
            themeColor: '#2a6cfb', // 全局主题色
            defaultAvatar: '',//require('../static/image/avatar.png'),
            uploadUrl: '', // 文件上传地址
            uploadHeader: {
                token: ''
            }
        }
    },
    onShow() {
        this._handleSavePageId();
    },
    onLoad() {
        this._handleInitOptions();
    },
    onUnload() {
        this._handleSaveOperTime();
    },
    onPageScroll(e) {
        this.scroll = e.scrollTop;
        this.scrollInfo = e.detail;
        this.$setState('scrollTop', e.scrollTop);
    },
    methods: {
        // 初始化配置
        _handleInitOptions() {
            let systemInfo = uni.getSystemInfoSync();
            let menuButtonInfo = {};

            // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
            menuButtonInfo = uni.getMenuButtonBoundingClientRect();
            Vue.prototype.menuButtonInfo = menuButtonInfo;
            // #endif

            Vue.prototype.systemInfo = systemInfo;
            Vue.prototype.statusBarHeight = systemInfo.statusBarHeight;
            // #ifdef APP-PLUS || H5
            Vue.prototype.navbarHeight = 44;
            // #endif
            // #ifdef MP
            Vue.prototype.navbarHeight = systemInfo.platform == 'ios' ? 44 : 48;
            // #endif

            this.uploadHeader.token = this.token;
            this.uploadUrl = this.baseUrl + '/api/mp/core/comm/uploadFile';
        },
        // 用户行为记录
        _handleSaveOperTime() {
            if (this.logId) {
                this.$api.util.saveOperTime({
                    id: this.logId
                }).then(res => {})
            }
        },
        _handleSavePageId() {
            this.$setState('pageId', this._uid);
        },
        // 更新用户信息(此方法用于更新用户信息为全局混入,每个页面实例都会有此方法,请注意不要重名)
        handleUpdateUserInfo() {
            if (this.login) {
                this.$api.user.userInfo().then(res => {
                    this.$setState('userInfo', res);
                })
            }
        }
    },
    computed: {
        ...mapState(['logId', 'token', 'baseUrl', 'login']),
        navbarStyle() {
            return {
                'padding-top': `${this.statusBarHeight}px`
            }
        },
        // 渐变色
        gradientColor() {
            const {
                r,
                g,
                b
            } = this.$hexToRgb(this.themeColor);
            return `rgba(${r},${g},${b},${this.scroll / 100})`;
        },
        // 渐变透明
        gradientOpacity() {
            const style = {};
            style.opacity = this.scroll / 100;
            return style;
        },
    },
}
